package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/depts")
@RestController
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

    //添加部门内容
    @Log
    @RequestMapping
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    //查询内容
    @GetMapping
    public Result list(String  name, Integer status,
                       @RequestParam (defaultValue = "1") Integer page,
                       @RequestParam (defaultValue = "10") Integer pageSize) {
        log.info("查询内容：{},{},{},{}",name,status,page,pageSize);
        PageResult<Dept> pageResult = deptService.list (name, status, page, pageSize);
        return Result.success(pageResult);
    }

    //根据ID查询部门
    @GetMapping("/{id}")
    public Result getByID(@PathVariable Integer id) {
        log.info("根据ID查询部门:{}",id);
        Dept dept = deptService.getByID(id);
        return Result.success(dept);
    }

    //修改部门内容
    @Log
    @PutMapping
    public Result upDate(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);
        deptService.upDate(dept);
        return Result.success();
    }

    //删除部门内容
    @Log
    @DeleteMapping("/{id}")
    public Result deletById(@PathVariable Integer id){
        log.info("删除部门:{}",id);
        deptService.deletById(id);
        return Result.success();
    }

    //查询所有部门
    @GetMapping("/list")
    public Result allList(Dept dept){
        log.info("查询所有部门:{}",dept);
        return deptService.allList(dept);
    }

}
